package com.tiantian.quanminxiu.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import com.tencent.rtmp.TXLiveConstants;
import com.tiantian.quanminxiu.R;
import com.tiantian.quanminxiu.module.ScrollHorizontalView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/11/27.
 */

public class LiveBeautyDialog extends Dialog implements SeekBar.OnSeekBarChangeListener
{
	public interface OnLiveBeautyCallBack
	{
		void onDialogDismiss(boolean dismiss);

		// style 磨皮风格： 0：光滑 1：自然 2：朦胧
		// beautyLevel 磨皮等级： 取值为0-9.取值为0时代表关闭美颜效果.默认值:0,即关闭美颜效果.6
		// whiteningLevel 美白等级： 取值为0-9.取值为0时代表关闭美白效果.默认值:0,即关闭美白效果.3
		// ruddyLevel 红润等级： 取值为0-9.取值为0时代表关闭美白效果.默认值:0,即关闭美白效果.3
		void onLiveBeauty(BeautyParams beautyParams);

		void onBeautyFilter(Bitmap filterBitmap);
	}

	public LiveBeautyDialog(Context context)
	{
		this(context, 0);
	}

	protected LiveBeautyDialog(Context context, boolean cancelable, OnCancelListener cancelListener)
	{
		super(context, R.style.dialogStyleLiveBottom);
		intialize(context);
	}

	Context mContext = null;

	public LiveBeautyDialog(Context context, int theme)
	{
		super(context, R.style.dialogStyleLiveBottom);
		setCanceledOnTouchOutside(true);
		setContentView(R.layout.dialog_live_beauty);
		mContext = context;

		Window dialogWindow = getWindow();
		dialogWindow.setGravity(Gravity.BOTTOM);
		WindowManager.LayoutParams params = dialogWindow.getAttributes();
		params.width = ViewGroup.LayoutParams.MATCH_PARENT;
		params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
		dialogWindow.setAttributes(params);

		intialize(context);
	}

	static public class BeautyParams
	{
		public int	mBeautyProgress	= 6;
		public int	mWhiteProgress	= 3;
		public int	mRuddyProgress	= 3;
		public int	mBeautyStyle	= TXLiveConstants.BEAUTY_STYLE_SMOOTH;
	}

	class FilterInfo
	{
		int		filterId;
		int		filterImage;
		String	filterText;
		boolean	isSelect;

		public FilterInfo(int filterId, int filterImage, String filterText)
		{
			super();
			this.filterId = filterId;
			this.filterImage = filterImage;
			this.filterText = filterText;
			isSelect = false;
		}
	}

	SeekBar					beautyPSeekbar	= null;
	SeekBar					whitenPSeekbar	= null;
	SeekBar					beautyLSeekbar	= null;
	SeekBar					whitenLSeekbar	= null;
	ArrayList<FilterInfo>	filterList;
	ItemAdapter				filterAdapter;

	private void intialize(final Context context)
	{
		// FrameLayout rootFrame = (FrameLayout) findViewById(R.id.rootFrame);
		beautyPSeekbar = (SeekBar) findViewById(R.id.beautyPSeekbar);
		whitenPSeekbar = (SeekBar) findViewById(R.id.whitenPSeekbar);
		beautyLSeekbar = (SeekBar) findViewById(R.id.beautyLSeekbar);
		whitenLSeekbar = (SeekBar) findViewById(R.id.whitenLSeekbar);
		ScrollHorizontalView filterPicker = (ScrollHorizontalView) findViewById(R.id.filterPicker);

		filterList = new ArrayList<>();
		filterList.add(new FilterInfo(0, R.drawable.orginal, "洁净"));// 无特效滤镜
		filterList.add(new FilterInfo(4, R.drawable.fennen, "暖灯"));// 粉嫩滤镜
		filterList.add(new FilterInfo(5, R.drawable.huaijiu, "追忆"));// 怀旧滤镜
		filterList.add(new FilterInfo(1, R.drawable.langman, "阳光"));// 浪漫滤镜
		filterList.add(new FilterInfo(7, R.drawable.qingliang, "清凉"));// 清凉滤镜
		filterList.add(new FilterInfo(2, R.drawable.qingxin, "静心"));// 清新滤镜
		filterList.add(new FilterInfo(8, R.drawable.rixi, "清新"));// 日系滤镜
		filterList.add(new FilterInfo(3, R.drawable.weimei, "晚霞"));// 唯美滤镜
		filterAdapter = new ItemAdapter(LayoutInflater.from(context), filterList);
		filterPicker.setAdapter(filterAdapter);

		// rootFrame.setOnClickListener(this);
		beautyPSeekbar.setOnSeekBarChangeListener(this);
		whitenPSeekbar.setOnSeekBarChangeListener(this);
		beautyLSeekbar.setOnSeekBarChangeListener(this);
		whitenLSeekbar.setOnSeekBarChangeListener(this);
	}

	public void setScreenMode(int screenMode)
	{
		LinearLayout portraitLayout = (LinearLayout) findViewById(R.id.portraitLayout);
		LinearLayout landscapeLayout = (LinearLayout) findViewById(R.id.landscapeLayout);
		portraitLayout.setVisibility(screenMode == 1 ? View.GONE : View.VISIBLE);
		landscapeLayout.setVisibility(screenMode == 1 ? View.VISIBLE : View.GONE);
	}

	OnLiveBeautyCallBack	mListener		= null;
	BeautyParams			mBeautyParams	= null;

	public void setLiveBeautyCallBack(BeautyParams params, OnLiveBeautyCallBack mListener)
	{
		mBeautyParams = params;
		this.mListener = mListener;

		// 当LiveBeautyDialog重置时，先刷新一遍配置
		if (this.mListener instanceof OnLiveBeautyCallBack)
			this.mListener.onLiveBeauty(mBeautyParams);

		beautyPSeekbar.setProgress(mBeautyParams.mBeautyProgress * beautyPSeekbar.getMax() / 9);
		whitenPSeekbar.setProgress(mBeautyParams.mWhiteProgress * whitenPSeekbar.getMax() / 9);
		beautyLSeekbar.setProgress(mBeautyParams.mBeautyProgress * beautyLSeekbar.getMax() / 9);
		whitenLSeekbar.setProgress(mBeautyParams.mWhiteProgress * whitenLSeekbar.getMax() / 9);
	}

	@Override
	public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
	{
		switch (seekBar.getId())
		{
			case R.id.beautyPSeekbar:
				mBeautyParams.mBeautyProgress = filtNumber(9, beautyPSeekbar.getMax(), progress);
				if (mListener instanceof OnLiveBeautyCallBack)
					mListener.onLiveBeauty(mBeautyParams);
				break;
			case R.id.whitenPSeekbar:
				mBeautyParams.mWhiteProgress = filtNumber(9, whitenPSeekbar.getMax(), progress);
				if (mListener instanceof OnLiveBeautyCallBack)
					mListener.onLiveBeauty(mBeautyParams);
				break;
			case R.id.beautyLSeekbar:
				mBeautyParams.mBeautyProgress = filtNumber(9, beautyLSeekbar.getMax(), progress);
				if (mListener instanceof OnLiveBeautyCallBack)
					mListener.onLiveBeauty(mBeautyParams);
				break;
			case R.id.whitenLSeekbar:
				mBeautyParams.mWhiteProgress = filtNumber(9, whitenLSeekbar.getMax(), progress);
				if (mListener instanceof OnLiveBeautyCallBack)
					mListener.onLiveBeauty(mBeautyParams);
				break;

			default:
				break;
		}
	}

	@Override
	public void onStartTrackingTouch(SeekBar seekBar)
	{
	}

	@Override
	public void onStopTrackingTouch(SeekBar seekBar)
	{
	}

	@Override
	public void show()
	{
		super.show();
		if (mListener != null)
			mListener.onDialogDismiss(false);
	}

	@Override
	public void dismiss()
	{
		super.dismiss();
		if (mListener != null)
			mListener.onDialogDismiss(true);
	}

	class ItemAdapter extends BaseAdapter
	{
		LayoutInflater		mInflater;
		List<FilterInfo>	filterList;

		public ItemAdapter(LayoutInflater mInflater, List<FilterInfo> filterList)
		{
			this.mInflater = mInflater;
			this.filterList = filterList;
		}

		public void updateAdapter(List<FilterInfo> audioList)
		{
			this.filterList = audioList;
			notifyDataSetChanged();
		}

		@Override
		public int getCount()
		{
			return filterList.size();
		}

		@Override
		public Object getItem(int position)
		{
			return filterList.get(position);
		}

		@Override
		public long getItemId(int position)
		{
			return position;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent)
		{
			if (convertView == null)
			{
				convertView = LayoutInflater.from(mContext).inflate(R.layout.item_filter_picker, null);
			}

			View itemFrame = convertView.findViewById(R.id.itemFrame);
			ImageView itemImage = (ImageView) convertView.findViewById(R.id.itemImage);

			final FilterInfo data = (FilterInfo) getItem(position);

			itemFrame.setVisibility(data.isSelect?View.VISIBLE:View.INVISIBLE);
			itemImage.setImageDrawable(mContext.getResources().getDrawable(data.filterImage));
			convertView.setOnClickListener(new View.OnClickListener()
			{
				@Override
				public void onClick(View view)
				{
					setItemClick(data);
				}
			});
			return convertView;
		}
	}

	private void setItemClick(FilterInfo data)
	{
		for (FilterInfo object : filterList)
		{
			object.isSelect = data == object;
		}
		filterAdapter.updateAdapter(filterList);

		if (mListener != null)
			mListener.onBeautyFilter(getFilterBitmap(mContext.getResources(), data.filterId));
	}

	/**
	 * 滤镜定义
	 */
	public static final int	filterType_NONE			= 0;	// 无特效滤镜
	public static final int	filterType_langman		= 1;	// 浪漫滤镜
	public static final int	filterType_qingxin		= 2;	// 清新滤镜
	public static final int	filterType_weimei		= 3;	// 唯美滤镜
	public static final int	filterType_fennen		= 4;	// 粉嫩滤镜
	public static final int	filterType_huaijiu		= 5;	// 怀旧滤镜
	public static final int	filterType_landiao		= 6;	// 蓝调滤镜
	public static final int	filterType_qingliang	= 7;	// 清凉滤镜
	public static final int	filterType_rixi			= 8;	// 日系滤镜
	public static final int	filterType_white		= 9;	// 美白滤镜

	private static Bitmap decodeResource(Resources resources, int id)
	{
		TypedValue value = new TypedValue();
		resources.openRawResource(id, value);
		BitmapFactory.Options opts = new BitmapFactory.Options();
		opts.inTargetDensity = value.density;
		return BitmapFactory.decodeResource(resources, id, opts);
	}

	public static Bitmap getFilterBitmap(Resources resources, int filterType)
	{
		Bitmap bmp = null;
		switch (filterType)
		{
			case filterType_langman:
				bmp = decodeResource(resources, R.drawable.filter_langman);
				break;
			case filterType_qingxin:
				bmp = decodeResource(resources, R.drawable.filter_qingxin);
				break;
			case filterType_weimei:
				bmp = decodeResource(resources, R.drawable.filter_weimei);
				break;
			case filterType_fennen:
				bmp = decodeResource(resources, R.drawable.filter_fennen);
				break;
			case filterType_huaijiu:
				bmp = decodeResource(resources, R.drawable.filter_huaijiu);
				break;
			case filterType_landiao:
				bmp = decodeResource(resources, R.drawable.filter_landiao);
				break;
			case filterType_qingliang:
				bmp = decodeResource(resources, R.drawable.filter_qingliang);
				break;
			case filterType_rixi:
				bmp = decodeResource(resources, R.drawable.filter_rixi);
				break;
			case filterType_white:
				bmp = decodeResource(resources, R.drawable.filter_rixi);
				break;
			default:
				bmp = null;
				break;
		}
		return bmp;
	}

	/**
	 * 根据比例转化实际数值为相对值
	 * 
	 * @param gear
	 *            档位
	 * @param max
	 *            最大值
	 * @param curr
	 *            当前值
	 * @return 相对值
	 */
	public static int filtNumber(int gear, int max, int curr)
	{
		return curr / (max / gear);
	}
}
